<template>
    <div class="banner">
      <el-col :span="24" class="action-bar">
        <!-- <span style="color:#888888;">轮播图管理:</span> -->
        <el-button type="primary" size="medium" @click="addBtnReset" style="margin: 0 20px;">新增</el-button>
      </el-col>

      <!--列表-->
      <el-table :data="recordList" highlight-current-row class="el-table-style">
        <el-table-column type="index" min-width="60">
        </el-table-column>
        <el-table-column prop="unit_name" label="公司名称" min-width="100">
        </el-table-column>
        <el-table-column prop="Invoice_tax_number" label="税号" min-width="100">
        </el-table-column>
        <el-table-column label="单位地址" min-width="100">
          <template slot-scope="scope">
            <p>{{scope.row.unit_province}}{{scope.row.unit_city}}{{scope.row.unit_area}}{{scope.row.unit_address}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="unit_phone" label="公司电话" min-width="100">
        </el-table-column>
        <el-table-column prop="bank_account" label="开户银行" min-width="100">
        </el-table-column>
        <el-table-column prop="bank_account_num" label="银行账号" min-width="100">
        </el-table-column>
        <el-table-column prop="email_address" label="电子邮箱" min-width="100">
        </el-table-column>
        <el-table-column label="默认抬头" min-width="100">
          <template slot-scope="scope">
            <p>{{scope.row.is_default==0?'否':'是'}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" min-width="100">
        </el-table-column>
        <el-table-column label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="getInvoiceLookUpDetail(scope.row.id)">编辑</el-button>
            <el-button type="danger" class="danger-btn" size="mini" @click="delInvoiceLookUp(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="footer-bar">
        <!-- <div class="batch"><el-button size="medium ">批量删除</el-button></div> -->
        <div class="page-code">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="pageTotal"
            :page-size="limit"
            @current-change="getInvoiceLookUpList"
          >
          </el-pagination>
        </div>
      </el-col>

      <!-- 新增 -->
      <el-dialog title="新增发票抬头" :visible.sync="addFormVisible" width="40%">
        <el-form label-width="80px">
          <el-form-item label="税号">
            <el-input v-model="addForm.tax_number" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="开户银行">
            <el-input v-model="addForm.bank_account" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="银行账号">
            <el-input v-model="addForm.bank_account_num" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱">
            <el-input v-model="addForm.email_address" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="默认抬头">
            <el-switch
              v-model="addForm.is_default"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="addCancelBtn">取消</el-button>
          <el-button type="primary" @click="addSubmitBtn">提交</el-button>
        </div>
      </el-dialog>


      <!-- 编辑 -->
      <el-dialog title="编辑发票抬头" :visible.sync="editFormVisible" width="40%">
        <el-form label-width="80px">
          <el-form-item label="税号">
            <el-input v-model="setForm.tax_number" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="开户银行">
            <el-input v-model="setForm.bank_account" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="银行账号">
            <el-input v-model="setForm.bank_account_num" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱">
            <el-input v-model="setForm.email_address" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="默认抬头">
            <el-switch
              v-model="setForm.is_default"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editCancelBtn">取消</el-button>
          <el-button type="primary" @click="editSubmitBtn">提交</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
    export default {
      name: "receipt_rise",
      data(){
        return{
          addFormVisible: false,
          editFormVisible: false,
          recordList:[],
          pageTotal: 0,
          limit: 10,
          unit_id: window.localStorage.getItem('unit_id'),

          addForm:{
            unit_id:'',
            tax_number:'',
            bank_account:'',
            bank_account_num:'',
            email_address:'',
            is_default: '1'
          },

          setForm: {
            id:'',
            unit_id:'',
            tax_number:'',
            bank_account:'',
            bank_account_num:'',
            email_address:'',
            is_default: '1'
          }
        }
      },
      mounted(){
        this.getInvoiceLookUpList();
      },
      methods:{
        //获取发票抬头列表
        async getInvoiceLookUpList(page){
          let params = {
            page: page||1,
            limit: this.limit
          }
          const res = await this.$api.getInvoiceLookUpList(params);
          if (res.data.level == 'success'){
            this.recordList = res.data.data;
            this.pageTotal = res.data.page_info.total;
          }else{
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //编辑按钮点击获取发票详情
        async getInvoiceLookUpDetail(id){
          const res = await this.$api.getInvoiceLookUpDetail({id:id});
          if (res.data.level == 'success'){
            this.setForm = {
              id: res.data.data.id,
              unit_id: this.unit_id,
              tax_number: res.data.data.Invoice_tax_number,
              bank_account:res.data.data.bank_account,
              bank_account_num:res.data.data.bank_account_num,
              email_address:res.data.data.email_address,
              is_default: res.data.data.is_default
            }
            this.editFormVisible = true;
          }else{
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //新增提交按钮
        async addSubmitBtn(){
          const res = await this.$api.setInvoiceLookUp(this.addForm);
          if (res.data.level == 'success'){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
            this.addCancelBtn();
            this.getInvoiceLookUpList();
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //新增按钮重置表单
        addBtnReset(){
          this.addForm = {
            unit_id:this.unit_id,
            tax_number:'',
            bank_account:'',
            bank_account_num:'',
            email_address:'',
            is_default: '1',
          };
          this.addFormVisible = true;
        },
        //新增取消按钮
        addCancelBtn(){
          this.addForm = {
            unit_id:this.unit_id,
            tax_number:'',
            bank_account:'',
            bank_account_num:'',
            email_address:'',
            is_default:'1',
          };
          this.addFormVisible = false;
        },
        //编辑提交按钮
        async editSubmitBtn(){
          const res = await this.$api.setInvoiceLookUp(this.setForm);
          if (res.data.level == 'success'){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
            this.editCancelBtn();
            this.getInvoiceLookUpList();
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
        },
        //编辑取消按钮
        editCancelBtn(){
          this.addForm = {
            id: '',
            unit_id:this.unit_id,
            tax_number:'',
            bank_account:'',
            bank_account_num:'',
            email_address:'',
            is_default:'1',
          };
          this.editFormVisible = false;
        },
        //删除发票抬头
        async delInvoiceLookUp(id){
          const res = await this.$api.delInvoiceLookUp({id:id});
          if (res.data.level == 'success'){
            this.$message({
              type: 'success',
              message: res.data.message
            });
            this.getInvoiceLookUpList();
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        }
      }
    }
</script>

<style lang='scss' scoped>
  .banner{
    .action-bar{
      background-color: #f2f2f2;
      height: 60px;
      margin: 10px 0px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      padding-left: 20px;
      .action-bar-input{
        width: 250px;
        margin-right: 14px;
      }
    }
    .el-table-style{
      width: 100%;
      border: 1px solid #f2f2f2;
    }
    .footer-bar{
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: #f2f2f2;
      padding: 8px;
      margin: 10px 0px;
      box-sizing: border-box;
    }
    .dialog-footer{
      text-align: center;
    }
  }
</style>

<style>
  .el-pagination.is-background .el-pager li,
  .el-pagination.is-background .btn-prev,
  .el-pagination.is-background .btn-next
  {
    background-color: #fff;
    border: 1px solid #f1f1f1;
    margin: 0;
  }
  .member .el-dialog__body{overflow: auto;}
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
